package com.ruiyun.app.libtts;

import android.content.Context;
import android.util.Log;
import com.baidu.tts.client.SpeechSynthesizer;
import java.util.Map;

/**
 * 该类是对SpeechSynthesizer的封装
 * <p>
 * Created by fujiayi on 2017/5/24.
 */

public class MySyntherizer implements UiMessageListener {
    private static volatile MySyntherizer a;

    public static MySyntherizer getInstance() {
        if (a == null) {
            synchronized (SpeechSynthesizer.class) {
                if (a == null) {
                    a = new MySyntherizer();
                }
            }
        }
        return a;
    }
    private UiMessageListener uiMessageListener;
    protected SpeechSynthesizer mSpeechSynthesizer;
    protected Context context;

    private static final String TAG = "NonBlockSyntherizer";

    protected static volatile boolean isInitied = false;

    private MySyntherizer() {


    }

    /**
     * 注意该方法需要在新线程中调用。且该线程不能结束。详细请参见NonBlockSyntherizer的实现
     *
     * @return 是否初始化成功
     */
    public boolean init(Context context,String appid, String appkey, String secretKey) {
        this.context = context;
        InitConfig config = new InitConfig(appid, appkey, secretKey);
        log("初始化开始");
        mSpeechSynthesizer = SpeechSynthesizer.getInstance();
        mSpeechSynthesizer.setContext(context);
        mSpeechSynthesizer.setSpeechSynthesizerListener(new MessageListener(this));
        // 请替换为语音开发者平台上注册应用得到的App ID ,AppKey ，Secret Key ，填写在SynthActivity的开始位置
        mSpeechSynthesizer.setAppId(config.getAppId());
        mSpeechSynthesizer.setApiKey(config.getAppKey(), config.getSecretKey());
        setParams(config.getParams());
        // 初始化tts
        int result = mSpeechSynthesizer.initTts(config.getTtsMode());
        if (result != 0) {
            log("【error】initTts 初始化失败 + errorCode：" + result);
            return false;
        }
        // 此时可以调用 speak和synthesize方法
        log("合成引擎初始化成功");
        isInitied = true;
        return true;
    }

    /**
     * 合成并播放
     *
     * @param text 小于1024 GBK字节，即512个汉字或者字母数字
     * @return =0表示成功
     */
    public int speak(String text) {
        if (!isInitied) {
            log("TTS 还未初始化");
            return -1;
        }
        log("speak text:" + text);
        return mSpeechSynthesizer.speak(text);
    }

    public void setParams(Map<String, String> params) {
        if (params != null) {
            for (Map.Entry<String, String> e : params.entrySet()) {
                mSpeechSynthesizer.setParam(e.getKey(), e.getValue());
            }
        }
    }

    public int stop() {
        return mSpeechSynthesizer.stop();
    }


    public void release() {
        log("MySyntherizer release called");
        if (!isInitied) {
            // 这里报错是因为连续两次 new MySyntherizer。
            // 必须第一次new 之后，调用release方法
            throw new RuntimeException("TTS 还未初始化");
        }
        mSpeechSynthesizer.stop();
        mSpeechSynthesizer.release();
        mSpeechSynthesizer = null;
        isInitied = false;
    }



    public void setUiMessageListener(UiMessageListener uiMessageListener) {
        this.uiMessageListener = uiMessageListener;
    }

    protected void log(String message) {
        Log.d(TAG, message);
    }

    @Override
    public void onError(String msg) {
        if (uiMessageListener != null) uiMessageListener.onError(msg);
    }

    @Override
    public void onSpeechFinish() {
        if (uiMessageListener != null) uiMessageListener.onSpeechFinish();
    }
}
